# import tkinter as tk
from tkinter import messagebox, PhotoImage, Tk, Button
from subprocess import Popen



class AppLauncher(Tk):
    def __init__(self):
        super().__init__()
        self.title("应用启动器")
        self.iconbitmap("yx.ico")
        self.app_paths = []
        self.read_place()
        self.bind("<Configure>", self.on_button_release)
        self.load_app_paths()
        self.create_widgets()

    def load_app_paths(self):
        # 假设app_paths.txt文件的每一行包含应用的名称、路径和图标路径，格式为 "应用名称:完整路径:图标路径"
        with open("app_paths.txt", "r") as f:
            for line in f:
                try:
                    name, path = [s.strip() for s in line.strip().split(";")]
                    self.app_paths.append((name, path))
                except:
                    try:
                        self.row, self.padx, self.pady = [
                            int(s.strip()) for s in line.strip().split(";")
                        ]
                    except:
                        self.row, self.padx, self.pady = 4, 5, 5

    def create_widgets(self):
        if self.x and self.y:
            self.geometry(f"+{self.x}+{self.y}")
        for index, (name, path) in enumerate(self.app_paths):
            icon_Flag = True
            # 加载并缩放图标
            try:
                icon_image = PhotoImage(file=f"../png/{name}.png")
            except:
                try:
                    icon_image = PhotoImage(file=f"./png/{name}.png")
                except:
                    icon_image = PhotoImage(file="None.png")
                    icon_Flag = False

            if icon_Flag:
                # 创建按钮，并使用图标作为背景
                app_button = Button(
                    self.master,
                    text=name,
                    image=icon_image,
                    activeforeground="blue",
                    compound="top",
                    command=lambda p=path: self.launch_app(p),
                )
                app_button.image = icon_image  # 保持对图标的引用，防止被垃圾回收
            else:
                app_button = Button(
                    self.master,
                    text=name,
                    image=icon_image,
                    activeforeground="blue",
                    font=20,
                    compound="center",
                    command=lambda p=path: self.launch_app(p),
                )
                app_button.image = icon_image  # 保持对图标的引用，防止被垃圾回收
            app_button.grid(
                row=index // self.row,
                column=index % self.row,
                padx=self.padx,
                pady=self.pady,
            )  # 假设每行显示3个按钮

    def launch_app(self, app_path):
        try:
            # 启动应用
            Popen(app_path)
            # 显示启动成功的消息框
            # messagebox.showinfo("成功", f"{app_path} 已启动")
        except Exception as e:
            # 显示错误消息框
            messagebox.showerror("错误", f"无法启动 {app_path}: {e}")

        # 获取窗口的宽度和高度
        self.window_width = self.winfo_width()
        self.window_height = self.winfo_height()

    def on_button_release(self,event):
        # 获取当前位置
        self.x = self.winfo_x()
        self.y = self.winfo_y()
        self.write_place()

    def read_place(self):
        try:
            with open("place.txt", "r") as f:
                place = f.read()
                self.x, self.y = place.strip().split(";")
        except:
            self.x, self.y = 0, 0

    def write_place(self):
        with open("place.txt", "w") as f:
            f.write(f"{self.x};{self.y}")


app = AppLauncher()
app.mainloop()
